package com.wechat.back.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wechat.back.mapper.CommentMapper;
import com.wechat.back.pojo.articles.Article;
import com.wechat.back.pojo.comment.ArticleComment;
import com.wechat.back.pojo.comment.PoiComment;
import com.wechat.back.pojo.tools.PageBean;
import com.wechat.back.service.CommentService;
import com.wechat.back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void addArticleComment(ArticleComment articleComment) {
        articleComment.setCreateTime(LocalDateTime.now());
        articleComment.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        String username = (String) map.get("username");
        articleComment.setUserId(userId);
        articleComment.setUsername(username);
        commentMapper.addArticleComment(articleComment);
    }

    @Override
    public void addPoiComment(PoiComment poiComment) {
        poiComment.setCreateTime(LocalDateTime.now());
        poiComment.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        String username = (String) map.get("username");
        poiComment.setUserId(userId);
        poiComment.setUsername(username);
        System.out.println(poiComment.getCommentContent()+ poiComment.getPoiId());
        commentMapper.addPoiComment(poiComment);
    }
    @Override
    public PageBean<ArticleComment> listArticleComment(Integer pageNum, Integer pageSize,String username,Integer commentId, Integer userId, Integer articleId) {
        //1.创建PageBean对象
        PageBean<ArticleComment> pb = new PageBean<>();
        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        List<ArticleComment> as = commentMapper.getArticleComment(username,commentId, userId, articleId);
        Page<ArticleComment> p = (Page<ArticleComment>) as;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public PageBean<PoiComment> listPoiComment(Integer pageNum, Integer pageSize,String username,Integer commentId,Integer userId,Integer poiId) {
        //1.创建PageBean对象
        PageBean<PoiComment> pb = new PageBean<>();
        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        List<PoiComment> as = commentMapper.getPoiComment(username,commentId, userId, poiId);
        Page<PoiComment> p = (Page<PoiComment>) as;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void deleteArticleComment(Integer commentId) {
        commentMapper.deleteArticleComment(commentId);
    }

    @Override
    public void deletePoiComment(Integer commentId) {
        commentMapper.deletePoiComment(commentId);
    }

    @Override
    public void updateArticleComment(ArticleComment articleComment) {
        articleComment.setUpdateTime(LocalDateTime.now());
        commentMapper.updateArticleComment(articleComment);
    }

    @Override
    public void updatePoiComment(PoiComment poiComment) {
        poiComment.setUpdateTime(LocalDateTime.now());
        commentMapper.updatePoiComment(poiComment);
    }

    @Override
    public void updateArticleLikeCount(Integer likeCount, Integer commentId) {
        commentMapper.updateArticleLikeCount(likeCount, commentId);
    }

    @Override
    public void updatePoiLikeCount(Integer likeCount, Integer commentId) {
        commentMapper.updatePoiLikeCount(likeCount,commentId);
    }
}
